package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.ChannelAffiliationService;
import com.iot08.admin.service.ChannelService;
import com.iot08.admin.service.MenuService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.admin.utils.RedisEnum;
import com.iot08.common.config.redis.RedisService;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.Channel;
import com.iot08.common.entity.admin.ChannelAffiliation;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.response.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @author: Rock
 * @Description: 渠道管理模块控制器
 * @time: 2020/8/13 18:42
 */
@Controller
@RequestMapping("channel")
public class ChannelController extends BaseController {

    @Resource
    private ChannelService channelService;
    @Resource
    private ChannelAffiliationService channelAffiliationService;
    @Resource
    private MenuService menuService;
    @Resource
    private RedisService redisService;

    /**
     * @Description: 渠道列表
     * @time: 2020/8/14 9:50
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page, Integer status) {
        ModelAndView mv = new ModelAndView();
        Page<Channel> pages = channelService.list(page, status, this.getSessionUser());
        mv.addObject("channels", getChannels());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("status", status);
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.addObject("menu", menuService.getByName("设备列表"));
        mv.addObject("deviceTotalTime", redisService.get(RedisEnum.DeviceTotalTime.getKey()));
        mv.setViewName("channel/channel_list");
        return mv;
    }

    /**
     * @Description: 添加页面
     * @time: 2020/8/17 17:21
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("channel/channel_add");
        return mv;
    }

    /**
     * @Description: 保存
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(Channel channel) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        String msg = channelService.check(null, channel.getName(), channel.getAccount());
        if (StringUtils.isEmpty(msg)) {
            channel.setCreator(user.getId());
            channelService.save(channel, this.getSessionUser(), this.getRemortIP());
            //更新完成后清除一下缓存的
            channelList.clear();

            mv.addObject("msg", "success");
        } else {
            mv.addObject("msg", msg);
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id, name, account
     * @time: 2020/9/3 15:14
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkAddOrEdit")
    public Response<Object> checkAddOrEdit(String id, String name, String account) {
        String msg = channelService.check(id, name, account);
        if (StringUtils.isNotEmpty(msg)) {
            return Response.failedResponse(msg);
        }
        return Response.successResponse();
    }

    /**
     * @Description: 编辑页面
     * @time: 2020/8/17 17:21
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id, Integer showType) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("channel", channelService.findById(id));
        if (Const.INT_1.equals(showType)) {
            mv.setViewName("channel/channel_show");
        } else {
            mv.setViewName("channel/channel_edit");
        }
        return mv;
    }

    /**
     * @Description: 保存
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(Channel channel) {
        ModelAndView mv = new ModelAndView();
        String msg = channelService.check(channel.getId(), channel.getName(), channel.getAccount());
        if (StringUtils.isEmpty(msg)) {
            channelService.update(channel, this.getSessionUser(), this.getRemortIP());
            //更新完成后清除一下缓存的 渠道商列表
            channelList.clear();
            mv.addObject("msg", "success");
        } else {
            mv.addObject("msg", msg);
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 删除
     * @time: 2020/8/14 9:50
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        Response<Object> objectResponse = channelService.updateDel(id, this.getSessionUser(), this.getRemortIP());
        if(objectResponse.getCode() == 200){
            //更新完成后清除一下缓存的 渠道商列表
            channelList.clear();
        }
        return objectResponse;

    }

    /**
     * 重置密码
     *
     * @param id
     * @time: 2020/10/13 18:02
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "resetPwd", method = RequestMethod.POST)
    public Response<Object> resetPwd(String id) {
        return channelService.resetPwd(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @Description: 更新状态
     * @time: 2020/8/14 9:50
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    public Response<Object> updateStatus(String id, Integer status) {
        channelService.updateStatus(id, status, this.getSessionUser(), this.getRemortIP());
        return Response.successResponse();
    }

    /*************************** 渠道归属管理 *********************************/

    /**
     * @param [page, status]
     * @Description: 渠道归属列表
     * @time: 2020/8/18 18:14
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("listAffiliation")
    public ModelAndView listAffiliation(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<ChannelAffiliation> pages = channelAffiliationService.list(page);
        mv.addObject("channelAffs", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("channel/channel_affiliation_list");
        return mv;
    }

    /**
     * @Description: 添加渠道归属页面
     * @time: 2020/8/17 17:21
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAddAff")
    public ModelAndView toAddAff() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("channels", channelService.listAll(this.getSessionUser()));
        mv.setViewName("channel/channel_affiliation_add");
        return mv;
    }

    /**
     * @Description: 保存渠道归属
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "addAff", method = RequestMethod.POST)
    public ModelAndView addAff(ChannelAffiliation channelAff) {
        ModelAndView mv = new ModelAndView();
        SysUser user = this.getSessionUser();
        if (user != null) {
            channelAff.setCreator(user.getId());
            channelAffiliationService.save(channelAff, user, this.getRemortIP());
            mv.addObject("msg", "success");
        } else {
            mv.addObject("msg", "failed");
        }
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param [id, name]
     * @time: 2020/9/8 14:35
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("checkChannelAffName")
    public Response<Object> checkChannelAffName(String id, String name) {
        ChannelAffiliation ca = channelAffiliationService.getByName(name);
        if (ca != null) {
            if (StringUtils.isEmpty(id)) {
                return Response.failedResponse();
            } else if (!id.equals(ca.getId())) {
                return Response.failedResponse();
            }
        }
        return Response.successResponse();
    }

    /**
     * @Description: 渠道归属编辑
     * @time: 2020/8/19 15:49
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEditAff")
    public ModelAndView toEditAff(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("channels", channelService.listAll(this.getSessionUser()));
        mv.addObject("channelAff", channelAffiliationService.getById(id));
        mv.setViewName("channel/channel_affiliation_edit");
        return mv;
    }

    /**
     * @Description: 保存渠道归属编辑
     * @time: 2020/8/17 17:29
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "editAff", method = RequestMethod.POST)
    public ModelAndView editAff(ChannelAffiliation channelAff) {
        ModelAndView mv = new ModelAndView();
        channelAffiliationService.update(channelAff, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @Description: 删除渠道归属
     * @time: 2020/8/19 15:53
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping("delAff")
    public Response<Object> delAff(String id) {
        return channelAffiliationService.updateDel(id, this.getSessionUser(), this.getRemortIP());
    }

}
